/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.orderView.dish;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.JPanelFlowLayoutComponent;
import com.ruipengkj.component.jdialog.BorderJPanel;
import com.ruipengkj.component.jdialog.IDialog;
/**
 * 说明：
 * 取单
 */
public class OrderObtainDialog extends IDialog implements ListSelectionListener {
	
	private static final long serialVersionUID = 1L;
	
	private JList name;//订单列表

	private OrderTablePanel OrderTablePanel;//table面板
	
	private JButton sureButton;//确定
	private JButton cancleButton;//取消
	
	private List<HungData> hungDataList;
	
	public int selectOrder = -1;//取出的单
	
	public OrderObtainDialog(List<HungData> hungDataList) {
		super((JDialog)null, "取单", true);
		
		this.setSize(500, 380);
		this.hungDataList = hungDataList;
		
		DefaultListModel listModel = new DefaultListModel();
		if (hungDataList != null)
		for (int i = 0, length = hungDataList.size(); i < length; ++i) {
			listModel.addElement(hungDataList.get(i).getOrderNameList()+"  ");
		}
		name = new JList(listModel);
		name.setOpaque(false);
		init();
		
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setVisible(true);
	}
	
	private void init() {
		OrderTablePanel = new OrderTablePanel();
		
		name.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		name.setSelectedIndex(0);
		name.setFixedCellHeight(50);
		name.addMouseListener(this);
		name.addListSelectionListener(this);
		
		sureButton = new JButton("确定");//确定
		cancleButton = new JButton("取消");//取消
		
		Util.setSize(sureButton, 100, 40);
		Util.setSize(cancleButton, 100, 40);
		
		sureButton.setFont(Util.FONT_20_PLAIN);
		cancleButton.setFont(Util.FONT_20_PLAIN);
		
		sureButton.addActionListener(this);
		cancleButton.addActionListener(this);

		JPanel jp1 = uiFactory.createBorderJPanel();
		jp1.add(name);
		Util.setSize(name, 130, this.getHeight());
		
		JPanel jp = new BorderJPanel(new BorderLayout());
		jp.add(jp1, BorderLayout.WEST);
		jp.add(OrderTablePanel, BorderLayout.CENTER);
		jp.add(new JPanelFlowLayoutComponent(new Component[]{
			sureButton, cancleButton
		}, FlowLayout.CENTER), BorderLayout.SOUTH);
		jp.setBackground(Color.white);
		this.add(jp);
		jp.setCursor(Util.CURSOR_DEFAULT);
		setTableData();
	}

	private void setTableData(){
		if (hungDataList != null)
		OrderTablePanel.setTCommodityList(hungDataList.get(name.getSelectedIndex()).getHungCommodityList());
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		if (e.getSource() == name && e.getClickCount() == 2) {
			selectOrder = name.getSelectedIndex();
			this.dispose();
			return;
		} else {
			setTableData();
		}
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == sureButton) {
			selectOrder = name.getSelectedIndex();
			this.dispose();
		} else if (e.getSource() == cancleButton) {
			selectOrder = -1;
			this.dispose();
		} else {
			super.actionPerformed(e);
		}
	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
		setTableData();
	}
	
	public static void main(String[] args) {
		new OrderObtainDialog(null);
	}
}
